﻿using Supore.APP.UI.Comon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;

namespace Supore.APP.UI.Controls.Notify
{
    /// <summary>
    /// LoginUINotify.xaml 的交互逻辑
    /// </summary>
    public partial class LoginUINotify : Window
    {
        static Window window = new Window();
        double msgTop = 0, msgLeft = 0;
        const double HEIGHT = 80;
        public double TopFrom
        {
            get; set;
        }
        public static List<LoginUINotify>_dialogs = new List<LoginUINotify>();
        public LoginUINotify()
        {
            InitializeComponent();
            this.Loaded += LoginUINotify_Loaded;
        }

        private void LoginUINotify_Loaded(object sender, RoutedEventArgs e)
        {
            LoginUINotify self = sender as LoginUINotify;
            
            if (self != null)
            {
                self.UpdateLayout();
                SystemSounds.Asterisk.Play();
                //window = WindowHelper.GetActiveWindow();
                double top = window.Top;
                DoubleAnimation animation = new DoubleAnimation();
                animation.Duration = new Duration(TimeSpan.FromMilliseconds(600));
                animation.From = top;
                animation.To = top + self.ActualHeight/2;
                self.BeginAnimation(Window.TopProperty,animation);

                Task.Run(() =>
                {
                    System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(1500));
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        animation = new DoubleAnimation();
                        animation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
                        animation.Completed += (s, a) => { self.Close(); _dialogs.Remove(self); };
                        animation.From = top + this.ActualHeight/2;
                        animation.To = top+5;
                        this.BeginAnimation(Window.TopProperty, animation);
                    }));
                });
                //Task.Factory.StartNew(delegate
                //{
                //    int seconds = 5;
                //    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(seconds));
                //    this.Dispatcher.Invoke(delegate
                //    {
                //        animation = new DoubleAnimation();
                //        animation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
                //        animation.Completed += (s, a) => { self.Close(); _dialogs.Remove(self); };
                //        animation.From = right - self.ActualWidth;
                //        animation.To = right;
                //        self.BeginAnimation(Window.LeftProperty, animation);
                //    });
                //});
            }
        }

        private static double GetTopFrom(double top)
        {
            double topFrom = top + HEIGHT;
            bool isContinueFind = _dialogs.Any(o=>o.TopFrom == topFrom);
            while (isContinueFind) 
            {
                topFrom += HEIGHT;
                isContinueFind = _dialogs.Any(o=>o.TopFrom == topFrom);
            }
            if (topFrom <= top)
                topFrom = top ;
            return topFrom;
        }

        public static void ShowMsg(Window win)
        {
            window = win;
            LoginUINotify dialog = new LoginUINotify();
            dialog.TopFrom = GetTopFrom(window.Top);
            //dialog.Left = (window.Left + window.ActualWidth / 4)+20;
            dialog.Left = (window.Left + 397 + ((400-dialog.Width)/2));
            _dialogs.Add(dialog);
            dialog.Show();
        }

        public static void Show(NotifyType type,string title,string content)
        {
            LoginUINotify dialog = new LoginUINotify();
           // dialog.Title.te
            switch (type)
            {
                case NotifyType.Info:
                    dialog.Icon.Text = "\ue730";
                    //dialog.Border1.Background = new SolidColorBrush(Color.FromArgb(0xff, 0x1e, 0x9f, 0xff));
                    //dialog.Icon.Foreground = new SolidColorBrush(Color.FromArgb(0xff, 0x1e, 0x9f, 0xff)); 
                    break;
                case NotifyType.Success:
                    dialog.Icon.Text = "\ue869";
                    //dialog.Border1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x96, 0x88));
                    dialog.Icon.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x96, 0x88)); break;
                case NotifyType.Warn:
                    dialog.Icon.Text = "\ue876";
                    //dialog.Border1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xB8, 0x00));
                    dialog.Icon.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xB8, 0x00)); break;
                case NotifyType.Error:
                    dialog.Icon.Text = "\ue854";
                    //dialog.Border1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0x57, 0x22));
                    dialog.Icon.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0x57, 0x22)); break;
                default:
                    dialog.Icon.Text = "\ue730";
                    //dialog.Border1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x1E, 0x9F, 0xFF));
                    dialog.Icon.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0x1E, 0x9F, 0xFF)); break;
            }

           // dialog.TopFrom = GetTopFrom();
            _dialogs.Add(dialog);
            dialog.Show();
        }

        public static void ShowInfo(string content)
        {
            Show(NotifyType.Info, "提示", content);
        }
        public static void ShowInfo(string title, string content)
        {
            Show(NotifyType.Info, title, content);
        }

        public static void ShowSuccess(string content)
        {
            Show(NotifyType.Success, "提示", content);
        }
        public static void ShowSuccess(string title, string content)
        {
            Show(NotifyType.Success, title, content);
        }

        public static void ShowWarning(string content)
        {
            Show(NotifyType.Warn, "提示", content);
        }
        public static void ShowWarning(string title, string content)
        {
            Show(NotifyType.Warn, title, content);
        }
        public static void ShowError(string content)
        {
            Show(NotifyType.Error, "提示", content);
        }
        public static void ShowError(string title, string content)
        {
            Show(NotifyType.Error, title, content);
        }

        public enum NotifyType
        {
            Info,
            Success,
            Warn,
            Error
        }

        private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
        {
            double top = window.Top;
            DoubleAnimation animation = new DoubleAnimation();
            animation.Duration = new Duration(TimeSpan.FromMilliseconds(600));
            animation.Completed += (s, a) => { this.Close(); };
            animation.From = top + this.ActualHeight;
            animation.To = top+10;
            this.BeginAnimation(Window.TopProperty,animation);
        }
    }
}
